package com.wetest.uia2.stub;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.view.accessibility.AccessibilityNodeInfo;
import androidx.test.filters.LargeTest;
import androidx.test.filters.SdkSuppress;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.SearchCondition;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.Until;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.uiautomator.BuildConfig;
import com.github.uiautomator.Service;
import com.googlecode.jsonrpc4j.ErrorResolver;
import com.googlecode.jsonrpc4j.JsonRpcServer;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@SdkSuppress(minSdkVersion = 19)
@RunWith(AndroidJUnit4.class)
/* loaded from: classes9.dex */
public class Stub {
    private static final int CUSTOM_ERROR_CODE = -32001;
    private static final int LAUNCH_TIMEOUT = 5000;
    int PORT = 9008;
    AutomatorHttpServer server = new AutomatorHttpServer(this.PORT);

    @Before
    public void setUp() throws Exception {
        UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        device.wakeUp();
        JsonRpcServer jrs = new JsonRpcServer(new ObjectMapper(), new AutomatorServiceImpl(), AutomatorService.class);
        jrs.setShouldLogInvocationErrors(true);
        jrs.setErrorResolver(new ErrorResolver() { // from class: com.wetest.uia2.stub.Stub.1
            @Override // com.googlecode.jsonrpc4j.ErrorResolver
            public ErrorResolver.JsonError resolveError(Throwable throwable, Method method, List<JsonNode> list) {
                String data = throwable.getMessage();
                if (!throwable.getClass().equals(UiObjectNotFoundException.class)) {
                    throwable.printStackTrace();
                    StringWriter sw = new StringWriter();
                    throwable.printStackTrace(new PrintWriter(sw));
                    data = sw.toString();
                }
                return new ErrorResolver.JsonError(Stub.CUSTOM_ERROR_CODE, throwable.getClass().getName(), data);
            }
        });
        this.server.route("/jsonrpc/0", jrs);
        this.server.start();
    }

    private void launchPackage(String packageName) {
        Log.i("Launch " + packageName);
        UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        Context context = InstrumentationRegistry.getInstrumentation().getContext();
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        intent.addFlags(268468224);
        context.startActivity(intent);
        device.wait((SearchCondition) Until.hasObject(By.pkg(packageName).depth(0)), 5000L);
        device.pressHome();
    }

    private void launchTestApp() throws RemoteException {
        UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        device.wakeUp();
        String launcherPackage = device.getLauncherPackageName();
        Boolean ready = (Boolean) device.wait((SearchCondition) Until.hasObject(By.pkg(launcherPackage).depth(0)), 5000L);
        if (!ready.booleanValue()) {
            Log.i("Wait for launcher timeout");
        } else {
            Log.d("Launch service");
        }
    }

    private void startMonitorService(Context context) {
        Intent intent = new Intent(Service.ACTION_START);
        intent.setPackage(BuildConfig.APPLICATION_ID);
        context.startService(intent);
    }

    @After
    public void tearDown() {
        this.server.stop();
    }

    private boolean checkAccessibilityQuery() throws InterruptedException {
        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
        for (int i = 3; i > 0; i--) {
            AccessibilityNodeInfo nodeInfo = instrumentation.getUiAutomation().getRootInActiveWindow();
            if (nodeInfo != null) {
                return true;
            }
            if (i > 1) {
                Thread.sleep(1000L);
            }
        }
        return false;
    }

    @LargeTest
    @Test
    public void testUIAutomatorStub() throws InterruptedException {
        Log.i("server started");
        while (this.server.isAlive()) {
            if (!checkAccessibilityQuery()) {
                Log.e("uiAutomation.getRootInActiveWindow() always return null, okhttpd server quit");
                return;
            }
            Thread.sleep(500L);
        }
    }
}